package com.glory.wxelectricitypda.activity;


import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.LabelListingAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.LabelListingBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Objects;
import java.util.function.Consumer;

import butterknife.BindView;

public class LabelListingActivity extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.storageId)
    FreeUI_ClearEditText storageId;
    @BindView(R.id.checkPackLotId)
    FreeUI_ClearEditText checkPackLotId;
    @BindView(R.id.suggestStorageId)
    TextView suggestStorageId;
    @BindView(R.id.rlv_list)
    RecyclerView rlv_list;
    public LabelListingAdapter mLabelListingAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_label_listing;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("标签上架");

        rlv_list.setLayoutManager(new LinearLayoutManager(LabelListingActivity.this));
        rlv_list.setItemAnimator(new DefaultItemAnimator());
        rlv_list.addItemDecoration(new DividerItemDecoration(LabelListingActivity.this, 1));
        mLabelListingAdapter = new LabelListingAdapter(new ArrayList<>());
        mLabelListingAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mLabelListingAdapter.isFirstOnly(true);
        rlv_list.setAdapter(mLabelListingAdapter);

    }

    @Override
    protected void initData() {
        checkPackLotId.requestFocus();
        initEdit(checkPackLotId,s->{
            if (storageId.getText().toString().isEmpty()){
                queryCheckPackLotId();
            }else {
                queryStorageId();
            }
        });
        initEdit(storageId,s->{
            if (storageId.getText().toString().isEmpty()){
                showToast("请扫描储位号！",false);
                return;
            }
            if (checkPackLotId.getText().toString().isEmpty()){
                showToast("请扫描条形码！",false);
                return;
            }
            queryStorageId();
        });


    }

    private void queryStorageId(){

        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "ITEMSHELF");
            map.put("CHECKPACKLOTID", checkPackLotId.getText().toString());
            map.put("STORAGEID", storageId.getText().toString());
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.LABELSTORAGE");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<LabelListingBean>>(
                new TypeToken<ResponseBean<LabelListingBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onSuccess(Response<ResponseBean<LabelListingBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    if (!checkDates(response.body().getBody().getXPACKAGEDETAIL().getPACKLOTID())){
                        showToast("该条码已添加！",false);
                        return;
                    }
                    mLabelListingAdapter.addData(0, response.body().getBody().getXPACKAGEDETAIL());
                    mLabelListingAdapter.notifyDataSetChanged();
//                    suggestStorageId.setText(response.body().getBody().getXPACKAGEDETAIL().getSUGGESTSTORAGEID());
                    checkPackLotId.setText("");
                    checkPackLotId.requestFocus();
                }
            }

            @Override
            public void onError(Response<ResponseBean<LabelListingBean>> response) {
                super.onError(response);
                loadError(response.getException(), "LABELSTORAGE");
            }
        });

    }


    private void queryCheckPackLotId(){

        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "QUERY");
            map.put("CHECKPACKLOTID", checkPackLotId.getText().toString());
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.LABELSTORAGE");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<LabelListingBean>>(
                new TypeToken<ResponseBean<LabelListingBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onSuccess(Response<ResponseBean<LabelListingBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    suggestStorageId.setText(response.body().getBody().getXPACKAGEDETAIL().getSUGGESTSTORAGEID());
                    if (!response.body().getBody().getXPACKAGEDETAIL().getSTORAGEID().isEmpty()){
                        if (!checkDates(response.body().getBody().getXPACKAGEDETAIL().getPACKLOTID())){
                            showToast("该条码已添加！",false);
                            return;
                        }
                        mLabelListingAdapter.addData(0, response.body().getBody().getXPACKAGEDETAIL());
                        mLabelListingAdapter.notifyDataSetChanged();
                        checkPackLotId.setText("");
                        checkPackLotId.requestFocus();
                    }else {
                        storageId.requestFocus();
                    }

                }
            }

            @Override
            public void onError(Response<ResponseBean<LabelListingBean>> response) {
                super.onError(response);
                loadError(response.getException(), "LABELSTORAGE");
            }
        });

    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    private boolean checkDates(String lotID){
        final boolean[] type = {true};
        mLabelListingAdapter.getData().forEach(new Consumer<LabelListingBean.XPACKAGEDETAILDTO>() {
            @Override
            public void accept(LabelListingBean.XPACKAGEDETAILDTO xpackagedetaildto) {
                if (Objects.equals(xpackagedetaildto.getPACKLOTID(), lotID)){
                    type[0] = false;
                }

            }
        });
        return type[0];
    }



    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header", llMain);
    }
}